package com.cwt.admin.manage;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.enums.RoleEnum;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.MD5Utils;
import com.cwt.common.utils.MapUtils;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.utils.StringUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.SysUser;
import com.cwt.service.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【】
 * @date 2023年08月11日 17:23:35
 */
@Api(tags = "用户管理", value = "UserController")
@RequestMapping("/user")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("system")
)
public class UserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取最新用户信息
     *
     * @param id 用户id
     * @return 最新用户信息
     */
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult nowUser(@PathVariable Long id) {
        return AjaxResult.success(sysUserService.getById(id));
    }

    /**
     * 用户数据分页
     *
     * @param info 分页数据信息，及条件信息
     * @return 用户分页数据
     */
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult page(@RequestBody PageInfo info) {
        Page<SysUser> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        return AjaxResult.success(sysUserService.page(page, queryWrapper));
    }

    /**
     * 根据用户id修改用户
     *
     * @param user 用户
     * @return 修改状态
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult editUser(@RequestBody SysUser user) {
        user.setPassword(MD5Utils.string2MD5(user.getPassword() + StringUtils.getComplexRandomString(16)));
        return AjaxResult.success(sysUserService.updateById(user));
    }

    /**
     * 根据用户id删除用户
     *
     * @param id 用户id
     * @return 删除状态
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult deleteUserById(@RequestBody Long id) {
        if (!sysUserService.removeById(id)) {
            return AjaxResult.error("用户删除失败");
        }
        MapUtils dParams = new MapUtils().put("id", id);
        return AjaxResult.success(sysUserService.removeByMap(dParams));
    }

}
